home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0065_Direct access to a Stream.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  2.1 KB  |  61 lines

  1. (*
  2. If you have the VCL source look at TMemoryStream - it is pretty efficient
  3. for 286 code.  If you are willing to only run on 386 machines you can use
  4. 386 32-bit addressing in assembly code to access the data.  Following the
  5. includion I have placed some assembly code I have written to do this.  It
  6. compiles under MASM and is linked with the {$L} directive.  The reason I
  7. wrote this code was for a WinG module I am still working on.  Anyone is
  8. welcome to use this code, but I would like to see work done using it.
  9.  
  10. >The TMemoryStream has a property TMemoryStream.Memory, which returns a
  11. >pointer to the actual location of the data in memory. To access the 
  12. >first byte I could just use TMemoryStream.Memory^
  13. >
  14. >The problem is: If I want to for example get bytes 15 to 21 out of the
  15. >stream, is there a way to point to the location of this data?
  16. >I do not want to use the Seek/Read function, as this is a lot slower
  17. >than using direct memory access.
  18. *)
  19.  
  20. {$L move.obj}
  21.  
  22. procedure Move32(pSrc: Pointer; srcOffset: Longint; pDest: Pointer;
  23. destOffset: Longint; len: Longint); external;
  24.  
  25. Assembly code (move.asm)
  26.     .MODEL large, PASCAL
  27.     .386
  28.     OPTION SCOPED
  29.  
  30. LPBYTE TYPEDEF FAR PTR BYTE
  31.  
  32. Move32 PROTO FAR PASCAL,
  33.          pSrc:LPBYTE, srcOffset:DWORD,
  34.          pDest: LPBYTE, destOffset:DWORD,
  35.          len:DWORD
  36.  
  37.     .Code
  38.  
  39. Move32    PROC FAR PASCAL USES ds es esi edi,
  40.         pSrc: LPBYTE, srcOffset:DWORD,
  41.         pDest: LPBYTE, destOffset:DWORD,
  42.         len:DWORD
  43.     cld            ;move forward through memory
  44.     mov    esi,0        ;clear index registers - noteably the top of
  45.     mov    edi,0        ;the 32 bit (upper 16 bits) registers.
  46.  
  47.     lds    si,pSrc        ;load ds/si with pointer to base source
  48.     les    di,pDest    ;load es/di with pointer to base destination
  49.     add    esi,srcOffset    ;add in offset to desired point
  50.     add    edi,destOffset    ;add in offset to desired point
  51.     mov    ecx,len        ;get the length of the move
  52.     shr    ecx,2        ;divide by 4 for DWORD moves
  53.     rep    movsd        ;if ecx is zero no move is done
  54.     mov    ecx,len        ;pick up length to do remainder
  55.     and    ecx,3        ;only move 0-3 bytes
  56.     rep    movsb        ;if ecx is zero no move is done
  57.     retf            ;far return
  58. Move32    ENDP
  59.  
  60. END
  61.